package model;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;

public class PicturesInfo implements Serializable {

    private static final long serialVersionUID = 4612836551111724376L;
    private JSONObject data;
    private int        size;

    /*
     * index: 封面
     * head: 头像
     * imgsCount: 附图个数
     * img_n: 第n个附图
     */

    public PicturesInfo() {
        data = new JSONObject();
        size = 0;
    }

    public PicturesInfo(String json) {
        try {
            data = JSONObject.parseObject(json);
            size = getCount();
        } catch (JSONException e) {
            data = null;
            size = 0;
        }
    }

    public PicturesInfo(JSONObject jsonObject) {
        data = new JSONObject(jsonObject);
        size = getCount();
    }

    public void setIndex(String picAddr) {
        if(picAddr != null) data.put("index", picAddr);
    }

    public void setHead(String picAddr) {
        if(picAddr != null) data.put("head", picAddr);
    }

    public void setImg(String picAddr) {
        if(picAddr != null) data.put("img_" + (size++), picAddr);
    }

    public void push(String picAddr) { setImg(picAddr); }

    public String pop() {
        String key  = "img_" + (--size);
        String last = data.getString(key);
        data.remove(key);

        return last;
    }

    private boolean swapValue(int idx_a, int idx_b) {
        if(idx_a < 0  ||  idx_b < 0  ||  idx_a == idx_b  ||  idx_a >= size  ||  idx_b >= size) return false;

        String K1 = "img_" + idx_a;
        String K2 = "img_" + idx_b;

        String V1 =  data.getString(K1);
        data.put(K1, data.getString(K2));
        data.put(K2, V1);

        return true;
    }

    public String removeImg(int idx) {
        if(idx < 0  ||  idx >= size) return null;
        swapValue(idx, size - 1);
        return pop();
    }

    ////////////////////////////////////////////////////////////////////////////

    /**
     * 获取封面图片地址信息
     * @return 封面图片地址信息
     */
    public String getIndex() {
        if(data == null) {
            return null;
        }
        return data.getString("index");
    }

    /**
     * 获取头像图片地址信息
     * @return 头像图片地址信息
     */
    public String getHead() {
        if(data == null) return null;
        return data.getString("head");
    }

    private int getCount() {
        if(data == null) return 0;

        try {
            return Integer.parseInt(data.getString("imgsCount"));
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    /**
     * 获取附图个数
     * @return 附图个数
     */
    public int getImgsCount() {
        return size;
    }

    /**
     * 获取附图
     * @param indx 附图下标（不包括封面和头像）
     * @return 附图地址信息
     */
    public String getImg(int indx) {
        if(data == null  ||  indx >= size) return null;
        return data.getString("img_" + indx);
    }

    @Override
    public String toString() {
        return data.toJSONString();
    }
}
